package com.ddyx.service.impl;

import java.util.Date;
import java.util.Map;

import javax.annotation.Resource;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.ObjectMessage;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ddyx.entity.CustomerContact;
import com.ddyx.entity.EventClickCart;
import com.ddyx.entity.EventClickPageview;
import com.ddyx.entity.EventClickSearch;
import com.ddyx.entity.EventLog;
import com.ddyx.mapper.CustomerContactMapper;
import com.ddyx.mapper.CustomerMapper;
import com.ddyx.mapper.EventClickCartMapper;
import com.ddyx.mapper.EventClickPageviewMapper;
import com.ddyx.mapper.EventClickSearchMapper;
import com.ddyx.mapper.EventLogMapper;
import com.ddyx.util.EventTag;
import com.ddyx.util.MyBeanUtils;

import lombok.extern.slf4j.Slf4j;

/**
 * @ClassName: MQUtilServiceImpl.java
 * @Description: mq同步队列的消费者,订阅消息,按操作类型进行数据同步操作
 * @author: LHj
 * @date: 2017年12月27日
 */

@Slf4j
@Service
public class MQEventListener implements MessageListener {
	@Autowired
	CustomerMapper customerMapper;
	@Resource
	EventLogMapper eventLogMapper;
	@Resource
	CustomerContactMapper ccMapper;
	@Resource
	EventClickSearchMapper ecsMapper;
	@Resource
	EventClickPageviewMapper ecpMapper;
	@Resource
	EventClickCartMapper eccMapper;
	
	
	@SuppressWarnings("unchecked")
	@Override
	public void onMessage(Message msg) {
		if (msg != null) {
			if (msg instanceof ObjectMessage) {
				ObjectMessage obMsg = (ObjectMessage) msg;
				try {
					Map<String, Object> map = (Map<String, Object>) obMsg.getObject();
					// 顾客预约商品
					if (map.containsKey("actionType") && map.get("actionType").equals(EventTag.BOOKING)) {
						EventLog eventLog = new EventLog();
						eventLog.setEventTag(map.get("actionType") + "");
						MyBeanUtils.populate(eventLog, map);
						eventLogMapper.insertSelective(eventLog);
					}
					// 顾客关注门店
					if (map.containsKey("actionType") && map.get("actionType").equals(EventTag.ATTENTIONSTORE)) {
						EventLog eventLog = new EventLog();
						eventLog.setEventTag(map.get("actionType") + "");
						MyBeanUtils.populate(eventLog, map);
						eventLogMapper.insertSelective(eventLog);
					}
					// 顾客取消关注门店
					if (map.containsKey("actionType") && map.get("actionType").equals(EventTag.CANCELATTENTIONSTORE)) {
						EventLog eventLog = new EventLog();
						eventLog.setEventTag(map.get("actionType") + "");
						MyBeanUtils.populate(eventLog, map);
						eventLogMapper.insertSelective(eventLog);
					}
					// 新增顾客同步
					if (map.containsKey("actionType") && map.get("actionType").equals(EventTag.NEWCUSTOMER)) {
						customerMapper.newCustomer(map);
					}
					// 添加顾客和导购关系
					if (map.containsKey("actionType") && map.get("actionType").equals(EventTag.CUSTCONTACT)) {
						CustomerContact cc = new CustomerContact();
						MyBeanUtils.populate(cc, map);
						ccMapper.addCustContact(cc);
					}
					// 搜索事件记录
					if (map.containsKey("actionType") && map.get("actionType").equals(EventTag.SEARCH)) {
						EventClickSearch search = new EventClickSearch();
						MyBeanUtils.populate(search, map);
						ecsMapper.insertSelective(search);
					}
					// 浏览记录
					if (map.containsKey("actionType") && map.get("actionType").equals(EventTag.BROWSE)) {
						EventClickPageview ecp = new EventClickPageview();
						MyBeanUtils.populate(ecp, map);
						ecpMapper.insertSelective(ecp);
					}
					// 添加购物车事件
					if(map.containsKey("actionType") && map.get("actionType").equals(EventTag.ADDCART)) {
						EventClickCart ecc = new EventClickCart();
						MyBeanUtils.populate(ecc, map);
						eccMapper.insertSelective(ecc);
					}
				} catch (Exception e) {
					System.err.println("[错误日志]-com.ddyx.service.impl.MQEventListener.java");
					System.err.println("[错误日志]-参数错误.............");
//					e.printStackTrace();
				}

			}
		}

	}

}
